#{extends 'backend_main.html' /}
    <div class="x-body">
        <form action="" method="post" class="layui-form layui-form-pane">
                <div class="layui-form-item layui-form-text">
                    <label class="layui-form-label">
                        拥有权限
                    </label>
                    <table  class="layui-table layui-input-block">
                        <tbody>
                            <tr>
                                <td class="mainTr">
                                    <input name="idRole[]" type="checkbox" value="${sysRole?.id}">
                                    ${sysRole?.role_name}
                                </td>
                                <td class="subTr">
                                    <div class="layui-input-block">
                                        #{list sysMenuList}
                                            <input name="idMenu[]" type="checkbox" value="${_?.id}"> ${_?.menu_name}
                                        #{/list}
                                    </div>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </div>
                <div class="layui-form-item">
                <button class="layui-btn" lay-submit="" lay-filter="save">保存</button>
              </div>
            </form>
    </div>
    <script>
        layui.use(['form','layer'], function(){
            $ = layui.jquery;
          var form = layui.form
          ,layer = layui.layer;
          //自定义验证规则
          form.verify({
          });
          //监听提交
          form.on('submit(save)', function(data){
              var obj = $(".subTr>.layui-input-block>.layui-form-checked");
              var arrTmp = new Array();
              obj.each(function () {
                  var tmp = $(this).prev("input").val();
                  arrTmp.push(tmp);
              });
              if(arrTmp.toString()==""){
                  layer.msg("至少选择一个选择项");
                  return false;
              }
              $.ajax({
                  type:"POST",                      //请求类型
                  url:"/SystemCenter/addPermission?v=insert",                 //URL
                  data:"id="+arrTmp.toString()+"&roleId=${sysRole?.id}",                  //传递的参数
                  dataType:"json",                 //返回的数据类型
                  success:function(data){          //data就是返回的json类型的数据
                      layer.alert("保存成功", {icon: 6},function () {
                          // 获得frame索引
                          var index = parent.layer.getFrameIndex(window.name);
                          //关闭当前frame
                          parent.layer.close(index);
                      });
                  }
              });
              return false;
          });
            function initCheckAll() {
                $(".mainTr>.layui-form-checkbox").click(function(event) {
                    if(!$(this).hasClass('layui-form-checked')){
                        $(this).removeClass('layui-form-checked');
                        $(".layui-form-checkbox").removeClass('layui-form-checked');
                    }else{
                        $(this).addClass('layui-form-checked');
                        $(".layui-form-checkbox").addClass('layui-form-checked');
                    }
                });
            }
            function initCheckedItem(){
                var obj = $(".subTr>.layui-input-block>.layui-form-checkbox");
                var idArray = "${menu_id}".split(',');
                obj.each(function () {
                    var tmp = $(this).prev("input").val();
                    if($.inArray(tmp, idArray)!=-1){
                        $(this).addClass('layui-form-checked');
                    }
                });
            }
            initCheckAll();
            initCheckedItem();
        });

    </script>
